/**
 * 
 */
package cn.edu.cqu.bysj.wm.ago.gn;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;

import cn.edu.cqu.bysj.wm.ago.anlys.Cluster;
import cn.edu.cqu.bysj.wm.ago.anlys.ModularityGN;
import cn.edu.cqu.bysj.wm.ago.anlys.Strength;
import cn.edu.cqu.bysj.wm.def.ConstDef;
import cn.edu.cqu.bysj.wm.def.Weight;
import cn.edu.cqu.bysj.wm.gui.result.DetectedResult;
import cn.edu.cqu.bysj.wm.neo4j.Neo4jGraphDb;
import cn.edu.cqu.bysj.wm.util.CommunityOutput;

/**
 * @author Meng
 * 
 */
public class GNRun {

	private static Neo4jGraphDb neo4jGraphDb, neo4jTreeDb;
	private static GraphDatabaseService gdb, tdb;

	/**
	 * initialize graph database service
	 */
	private static void initDbService() {
		neo4jGraphDb = new Neo4jGraphDb(ConstDef.GN_DB_PATH);
		gdb = neo4jGraphDb.db;

		neo4jTreeDb = new Neo4jGraphDb(ConstDef.TREE_DB_PATH);
		tdb = neo4jTreeDb.db;
	}

	/**
	 * close graph database service
	 */
	public static void closeDbService() {
		neo4jGraphDb.shutDownDb();
	}

	/**
	 * run GN algorithm
	 * 
	 * @param filePath
	 * @param weight
	 */
	public static void runGN(String filePath, Weight weight) {
		initDbService();

		GN gn = new GN(filePath, gdb, tdb, weight);

		gn.IterationGN();

		GNCommunity.showCommunity(gn.tree);

		ModularityGN.calculate(gn);
		Cluster.calculate(Direction.BOTH, ConstDef.GN);
		Strength.judge(Direction.BOTH, ConstDef.GN);

		DetectedResult.setGNAlgorithmResult(gn);
		CommunityOutput.outputGN(gn);

		// neo4jGraphDb.shutDownDb();
		neo4jTreeDb.shutDownDb();
	}
}
